/*select.c*/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int fds[2];
    char buf[7];
    int i, rc, maxfd;
    fd_set inset1, inset2;
    struct timeval tv;
    // 首先按一定的权限打开hello1文件
    if ((fds[0] = open("hello1",O_RDWR|O_CREAT,0666)) < 0)
        perror("open hello1");
    // 再按一定权限打开hello2文件
    if ((fds[1] = open("hello2",O_RDWR|O_CREAT,0666)) < 0)
        perror("open hello2");
    if ((rc = write(fds[0],"Hello!\n",7)))
        printf("rc=%d\n",rc);
    lseek(fds[0],0,SEEK_SET);
    // 取出文件描述符中的较大者
    maxfd = fds[0] > fds[1] ? fds[0] : fds[1];
    // 初始化读集合inset1,并在集合中加入相应的描述集合
    FD_ZERO(&inset1);
    FD_SET(fds[0],&inset1);
    // 初始化写集合inset2,并在写集合中加入相应的描述符
    FD_ZERO(&inset2);
    FD_SET(fds[1],&inset2);
    tv.tv_sec = 2;
    tv.tv_usec = 0;
    // 循环测试该文件描述符是否准备就绪，并调用select函数对相关文件描述符做出对于操作
    while(FD_ISSET(fds[0],&inset1) || FD_ISSET(fds[1],&inset2))
    {
        if (select(maxfd+1,&inset1,&inset2,NULL,&tv) < 0)
            perror("select");
        else
        {
            if (FD_ISSET(fds[0],&inset1)){
                rc = read(fds[0],buf,7);
                if (rc > 0){
                    buf[rc] = '\0';
                    printf("read %s \n",buf);
                }else
                    perror("read");
            }
            if(FD_ISSET(fds[1],&inset2)){
                rc = write(fds[1],buf,7);
                if (rc > 0){
                    buf[rc] = '\0';
                    printf("rc = %d,write: %s\n",rc,buf);
                }else
                    perror("write");
                sleep(10);
            }
        }
    }
    exit(0);
}